<template>
  <div class="followAdd" v-loading="detailInfoShow">
   
    <div class="follow-info">
          <el-form
            :model="followForm"
            ref="followFormRef"
            :rules="queryRules"
            label-width="130px"
            v-enterToNext
            :disabled="pageType === 'detail'"
          >
            <el-row>
              <el-col :span="24">
                <el-form-item label="项目启动时间：" prop="projectStartTime">
                  <el-date-picker
                    value-format="YYYY-MM-DD HH:mm:ss"
                    v-model="followForm.projectStartTime"
                    class="search-input"
                    type="datetime"
                    placeholder="请选择"
                  />
                </el-form-item>
              </el-col>
            </el-row>
           
            <el-col :span="24">
              <el-form-item label="项目结束时间：" prop="projectEndTime">
                  <el-date-picker
                    value-format="YYYY-MM-DD HH:mm:ss"
                    v-model="followForm.projectEndTime"
                    class="search-input"
                    type="datetime"
                    placeholder="请选择"
                  />
                </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="项目经理：" prop="pmList">
                <el-select
                  filterable
                    style="width: 100%"
                    v-model="followForm.pmList"
                    multiple
                    placeholder="请选择"
                    clearable
                    class="search-input"
                  >
                  <el-option key="0" label="" value="" disabled>
                      <span style="float: left; color: #8492a6">人员姓名</span>
                      <span style="margin: 0 80px 0 50px; color: #8492a6"
                        >联系方式</span
                      >
                      <span
                        style="
                          margin: 0 100px 0 0px;
                          color: #8492a6;
                          font-size: 13px;
                        "
                        >职位</span
                      >
                      <span style="color: #8492a6; font-size: 13px">部门</span>
                    </el-option>
                    <el-option
                      v-for="item in userList"
                      :key="item.userId"
                      :label="item.nickName"
                      :value="item.userId"
                    >
                      <span
                        style="display: inline-block; width: 80px; float: left"
                        >{{ item.nickName }}</span
                      >
                      <span
                        style="
                          display: inline-block;
                          width: 125px;
                          margin: 0 15px 0 23px;
                          color: #8492a6;
                          white-space: nowrap;
                          overflow: hidden;
                          text-overflow: ellipsis;
                        "
                        >{{ item.phonenumber ? item.phonenumber : "--" }}</span
                      >
                      <span
                        style="
                          display: inline-block;
                          width: 125px;
                          font-size: 13px;
                          margin: 0 0px 0 0px;
                          transform: translateY(-11px);
                          color: #8492a6;
                        "
                      >
                        {{ item.postName ? item.postName : "--" }}
                      </span>
                      <span
                        style="
                          display: inline-block;
                          margin: 0 0px 0 0px;
                          font-size: 13px;
                          transform: translateY(-11px);
                          color: #8492a6;
                        "
                      >
                        {{ item.fullDeptName ? item.fullDeptName : "--" }}
                      </span>
                    </el-option>
                  </el-select>
                </el-form-item>
            </el-col>
          </el-form>
        </div>
    <!-- 筛选按钮 -->
    <div v-if="pageType !== 'detail' && pageType !== 'check'" class="allbutton">
      <el-button
        v-if="pageType !== 'detail' && pageType !== 'check'"
        style="color: #fff; background-color: rgba(67, 207, 124, 1)"
        @click="handleAddSave(followFormRef)"
        >确认</el-button
      >
      <el-button  style="background-color: #fff" @click="handleBack">取消</el-button>
    </div>

    <el-dialog
      :title="'跟进记录'"
      v-model="followshow"
      width="1400px"
      append-to-body
    >
      <Followdetail v-if="followshow" ref="followdetail"></Followdetail>
    </el-dialog>
  </div>
</template>

<script setup>
import {
  getEndFollowApi,
  addFollowApi,
  getProjectApi,
} from "@/api/sales/project";
import { updatefollow } from "@/api/sales/marketing.js";
import Followdetail from "./followdetail";
import { getUserListApi } from "@/api/operation/work";
import { ElMessage, ElMessageBox } from "element-plus";
import { startReadyProjectApi } from "@/api/operation/project";
const emit = defineEmits([
  // 刷新父组件表格数据
  "refreshList",
  // 更新跟进组件显隐
  "update:followAddShow",
  //处理编辑
  "handleredit",
]);
const props = defineProps({
  // 操作类型
  pageType: {
    type: String,
    default: "add",
  },
});
// 折叠栏默认展开
const activeNames = ref(["1", "2", "3"]);
// 客户表单引用
const followFormRef = ref();
// 详情加载效果
const detailInfoShow = ref(false);
// 输入框引用
const refInput = ref();

let followshow = ref(false);
//数据
let rowdata = ref({
  projectName: "",
  projectCode: "",
});
// 跟进表单引用
const followFormTemplate = {
  "pmList": [],
  "projectEndTime": "",
  "projectId": null,
  "projectStartTime": ""
};
const data = reactive({
  // 跟进表单
  followForm: JSON.parse(JSON.stringify(followFormTemplate)),
  // 跟进表单校验规则
  queryRules: {
    projectStartTime: [
      { required: true, message: "请选择项目启动时间", trigger: "change" },
    ],
    projectEndTime: [
      { required: true, message: "请选择项目结束时间", trigger: "change" },
    ],
    pmList: [{ required: true, message: "请选择项目经理", trigger: "change" }],
  },
   // 用户列表
   userList: [],
});
const { followForm, queryRules,userList } = toRefs(data);

/**
 * @description: 保存
 * @param {Object} followFormRef 跟进表单引用
 */
const handleAddSave = async (followFormRef) => {
  if (!followFormRef) return;
  await followFormRef.validate(async (valid, fields) => {
    if (valid) {
      ElMessageBox.confirm("确定保存以上数据吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        confirmButtonClass: "confirmBtnClass",
        cancelButtonClass: "cancelBtnClass",
      })
        .then(async () => {
          const res = await startReadyProjectApi(followForm.value);
          if (res.code === 200) {
            emit("update:followAddShow", false);
            emit("refreshList", false);
            ElMessage.success(res.msg);
            followForm.value = JSON.parse(JSON.stringify(followFormTemplate));
          } else {
            ElMessage.error(res.msg);
          }
        })
        .catch(() => {});
    }
  });
};

/**
 * @description: 返回
 */
const handleBack = () => {
  ElMessageBox.confirm("确定返回上一级吗?以上数据未保存将会丢失!", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
    confirmButtonClass: "confirmBtnClass",
    cancelButtonClass: "cancelBtnClass",
  })
    .then(async () => {
      emit("update:followAddShow", false);
      followForm.value = JSON.parse(JSON.stringify(followFormTemplate));
    })
    .catch(() => {});
};

/**
 * @description: 处理详情信息
 * @param {Number} id
 */
const handleDetailInfo = async (row) => {
  rowdata.value = row;
  console.log("zhixng ", row);
  try {
    console.log("1");
    followForm.value.projectId = row.projectId;
    console.log(2);
    // if (props.pageType === "follow" || props.pageType === "isfollow") {
    //   console.log(3);
    //   const res = await getEndFollowApi(row.projectId);
    //   if (res) {
    //     console.log(4);
    //     console.log("res");
    //     followForm.value.followTime = res.createTime;
    //   }
    // } else {
    //   console.log("2");
    //   const res = await getProjectApi(row.projectId);
    //   followForm.value.tableData = res.proFollowUpRecordInfoVos;
    // }
  } catch (error) {}
};

/**
 * @description: 获取用户列表
 */
 const getUserList = async (id) => {
  await getUserListApi(2).then((res) => {
    if (res) {
      userList.value = res;
    }
  });
};

/**
 * @description: 处理跟进详情信息
 * @param {Number} id
 */
const followdetail = ref(null);
const handlerdetail = (row) => {
  followshow.value = true;
  setTimeout(() => {
    followdetail.value.handleDetailInfo({ ...row, ...rowdata.value });
  }, 100);
};

/**
 * @description: 处理编辑信息
 * @param {Number} id
 */
const handleredit = (row) => {
  console.log("执行");
  emit("handleredit", { ...row, ...rowdata.value });
};

let pageedit = ref("");
const editInfo = (data) => {
  console.log("编辑", data);
  rowdata.value = data;
  followForm.value = data;
  pageedit.value = "edit";
};

onMounted(() => {
  getUserList();
  if (props.pageType !== "detail" && props.pageType !== "check") {
    nextTick(() => {
      setTimeout(() => {
        refInput.value.focus();
      }, 800);
    });
  }
});

defineExpose({
  handleDetailInfo,
  editInfo,
});
</script>
<style lang="scss" scoped>
.followAdd {
  width: 100%;
  height: 100%;

  .follow-info {
    // padding: 20px 10px 5px 10px;
    // margin: 10px 0;
    .custom-textarea {
      :deep(.el-textarea__inner) {
        max-height: 200px !important; /* 你可以根据需要调整这个值 */
      }
    }
  }
  .follow-detail-info {
    margin-top: 20px;
    .table-operate {
      display: flex;
      justify-content: space-between;
      align-items: center;
      color: #437dfb;
      .line {
        width: 1px;
        height: 20px;
        background-color: #437dfb;
      }
    }
  }
  .allbutton {
    display: flex;
    justify-content: flex-end;
    margin-top: 10px;
  }
 
}
:deep(.el-collapse-item__arrow) {
  visibility: hidden;
}
</style>
<style lang="scss">
.followAdd {
  .el-popper.is-dark {
    max-width: 800px !important;
  }
}
</style>
